 <?php

class Cab_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

    public function preDispatch(Zend_Controller_Request_Abstract $request) {

        // Load ACL config
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
        $acl = new Cab_Acl_Acl($config); // new Application_Acl($config);
        // Begin authorisation
        $auth = Zend_Auth::getInstance();
        $role = 'guest';

        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();

            print_r($user);

            if (is_object($user)) {
                $role = $user->role;
            }
        }

        $controller = $request->getControllerName();
        $action = $request->getActionName();
        $module = $request->getModuleName();

        echo "<h2>".$controller. "</h2>";
        
        $resource = $controller;
        $privellege = $action;

        if (!$acl->has($resource)) {
            throw new Exception('No resource found');
        }

        if (!$acl->isAllowed($role, $resource, $privellege)) {
            $request->setModuleName('default')
                    ->setControllerName('user')
                    ->setActionName('login')
                    ->setDispatched(false);
        }
    }

}

